package com.gengzp.common.enums;

import com.gengzp.common.exception.BusinessException;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

/**
 * @ClassName YesNoEnum
 * @Description 是或否枚举类
 * @Author gengzp
 * @Date 2025/5/11 2:03
 */
@Getter
public enum YesNoEnum {

    NO(0, "否"),
    YES(1, "是");

    private final int code;
    private final String value;

    YesNoEnum(int code, String value) {
        this.code = code;
        this.value = value;
    }

    /**
     * 根据编码获取枚举值
     */
    public static String getValueByCode(int code) {
        for (YesNoEnum v : YesNoEnum.values()) {
            if (Objects.equals(v.getCode(), code)) {
                return v.getValue();
            }
        }
        throw BusinessException.get(String.format("枚举值不关联, 枚举类: %s,枚举值: %s", YesNoEnum.class, code));
    }

    /**
     * 根据枚举值获取编码
     */
    public static int getCodeByValue(String value) {
        for (YesNoEnum v : YesNoEnum.values()) {
            if (StringUtils.equals(v.getValue(), value)) {
                return v.getCode();
            }
        }
        throw BusinessException.get(String.format("枚举值不关联, 枚举类: %s,枚举值: %s", YesNoEnum.class, value));
    }

}
